package com.web.base.wechat.provider;

import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.wechat.exception.WechatMenuException;
import com.web.base.wechat.facade.WechatMenuFacade;
import com.web.base.wechat.message.WechatMenuRequest;
import com.web.base.wechat.message.WechatMenuResponse;
import com.web.base.wechat.model.WechatMenu;
import com.web.base.wechat.service.WechatMenuService;
import com.web.base.wechat.util.WechatMenuConvertor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 微信菜单接口实现类
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2017年04月27日
 */
@Component
public class WechatMenuProvider implements WechatMenuFacade
{
	@Autowired
	private WechatMenuService wechatMenuService;
	
	public WechatMenuProvider()
	{
	}

	@Override
	public WechatMenuResponse add(WechatMenuRequest request)
	{
		ValidationUtil.valid(request, SaveModel.class);
		WechatMenu wechatMenu = WechatMenuConvertor.toWechatMenu(request);
		wechatMenu = wechatMenuService.save(wechatMenu);
		WechatMenuResponse response = WechatMenuConvertor.toResponse(wechatMenu);
		return response;
	}

	@Override
	public WechatMenuResponse update(WechatMenuRequest request)
	{
		ValidationUtil.valid(request, UpdateModel.class);
		WechatMenu wechatMenu = WechatMenuConvertor.toWechatMenu(request);
		wechatMenu = wechatMenuService.save(wechatMenu);
		WechatMenuResponse response = WechatMenuConvertor.toResponse(wechatMenu);
		return response;
	}

	@Override
	public WechatMenuResponse get(Integer id)
	{
		if (id == null)
			throw new WechatMenuException("ID不能为空");
			
		WechatMenu wechatMenu = wechatMenuService.get(id);
		WechatMenuResponse response = WechatMenuConvertor.toResponse(wechatMenu);
		return response;
	}

	@Override
	public WechatMenuResponse getAll(Integer id)
	{
		if (id == null)
			throw new WechatMenuException("ID不能为空");

		WechatMenu wechatMenu = wechatMenuService.getAll(id);
		WechatMenuResponse response = WechatMenuConvertor.toResponse(wechatMenu);
		return response;
	}

	@Override
	public Map<String, List<WechatMenuResponse>> listMenu(Integer wechatId)
	{
		if (wechatId == null)
			throw new WechatMenuException("微信ID不能为空");
		
		WechatMenu wechatMenu = new WechatMenu();
		wechatMenu.setWechatId(wechatId);
		Map<String, List<WechatMenu>> map = wechatMenuService.listMenu(wechatMenu);
		Map<String, List<WechatMenuResponse>> responseMap = WechatMenuConvertor.toResponse(map);
		return responseMap;
	}

	@Override
	public Map<String, List<WechatMenuResponse>> listRootMenu()
	{
		Map<String, List<WechatMenu>> map = wechatMenuService.listRootMenu();
		Map<String, List<WechatMenuResponse>> responseMap = WechatMenuConvertor.toResponse(map);
		return responseMap;
	}
	
}
